<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Stringable 接口</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="weakmap.offsetunset.html">« WeakMap::offsetUnset</a></li>
      <li style="float: right;"><a href="stringable.tostring.html">Stringable::__toString »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="reserved.interfaces.html">预定义接口和类</a></li>
    <li>Stringable 接口</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="class.stringable" class="reference">

 <h1 class="title">Stringable 接口</h1>
 

 <div class="partintro"><p class="verinfo">(PHP 8)</p>


  <div class="section" id="stringable.intro">
   <h2 class="title">简介</h2>
   <p class="para">
    <span class="interfacename"><strong class="interfacename">Stringable</strong></span> 接口表示拥有 
    <a href="language.oop5.magic.html#object.tostring" class="link">__toString()</a> 方法的类。
    和大多数接口不同， <span class="interfacename"><strong class="interfacename">Stringable</strong></span> 隐式存在于任何定义了
    <a href="language.oop5.magic.html#object.tostring" class="link">__toString()</a> 魔术方法的类上，
    当然也可以显式声明它，并且推荐这么做。
   </p>
   <p class="para">
    它的主要价值是能让函数针对简单的字符串或可以转化为字符串的对象，检测联合类型 <code class="literal">string|Stringable</code>。
   </p>
  </div>


  <div class="section" id="stringable.synopsis">
   <h2 class="title">接口摘要</h2>


   <div class="classsynopsis">
    <div class="ooclass">
     
    </div>

    <div class="classsynopsisinfo">
     <span class="oointerface">
      <span class="modifier">interface</span> <strong class="interfacename">Stringable</strong></span> {</div>

    <div class="classsynopsisinfo classsynopsisinfo_comment">/* 方法 */</div>
    <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><a href="stringable.tostring.html" class="methodname">__toString</a></span>(): <span class="type">string</span></div>

   }</div>


  </div>

  <div class="section" id="stringable.examples">
   <h2 class="title">Stringable 示例</h2>
   <p class="para">
    <div class="example" id="stringable.basic-example">
     <p><strong>示例 #1 基础 Stringable 用法</strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">IPv4Address&nbsp;</span><span style="color: #007700">implements&nbsp;</span><span style="color: #0000BB">Stringable&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;</span><span style="color: #0000BB">string&nbsp;$oct1</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;</span><span style="color: #0000BB">string&nbsp;$oct2</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;</span><span style="color: #0000BB">string&nbsp;$oct3</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;</span><span style="color: #0000BB">string&nbsp;$oct4</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">__construct</span><span style="color: #007700">(</span><span style="color: #0000BB">string&nbsp;$oct1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">string&nbsp;$oct2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">string&nbsp;$oct3</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">string&nbsp;$oct4</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">oct1&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$oct1</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">oct2&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$oct2</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">oct3&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$oct3</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">oct4&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$oct4</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">__toString</span><span style="color: #007700">():&nbsp;</span><span style="color: #0000BB">string&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">oct1</span><span style="color: #DD0000">.</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">oct2</span><span style="color: #DD0000">.</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">oct3</span><span style="color: #DD0000">.</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">oct4</span><span style="color: #DD0000">"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br />function&nbsp;</span><span style="color: #0000BB">showStuff</span><span style="color: #007700">(</span><span style="color: #0000BB">string</span><span style="color: #007700">|</span><span style="color: #0000BB">Stringable&nbsp;$value</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;通过在此处调用&nbsp;__toString，Stringable&nbsp;将会获得转化后的字符串<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">print&nbsp;</span><span style="color: #0000BB">$value</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$ip&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">IPv4Address</span><span style="color: #007700">(</span><span style="color: #DD0000">'123'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'234'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'42'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'9'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">showStuff</span><span style="color: #007700">(</span><span style="color: #0000BB">$ip</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

     <div class="example-contents"><p>以上例程的输出类似于：</p></div>
     <div class="example-contents screen">
<div class="cdata"><pre>
123.234.42.9
</pre></div>
     </div>
    </div>
   </p>
  </div>

 </div>

 







<h2>目录</h2><ul class="chunklist chunklist_reference"><li><a href="stringable.tostring.html">Stringable::__toString</a> — 从对象中获取字符串表示</li></ul>
</div>
</div></div></body></html>